package org.kjk.mail.hanmail;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.kjk.mail.MailDTO;
import org.kjk.mail.gmail.GMailDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HanMailController {

	@Resource(name = "hanmail")
	public HanMail hanmail;

	@RequestMapping(value = "/readHMail.do", method = RequestMethod.GET)
	public ModelAndView readMail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("Page Loading");

		@SuppressWarnings("unchecked")
		List<MailDTO> mail = (List<MailDTO>) request.getSession()
				.getAttribute("mail");
		String refresh = request.getParameter("refresh");
		if (mail == null || (refresh != null && refresh.equals("true"))) {
			System.out.println("refresh mail list");
			mail = hanmail.getSubject();
		}
		if (mail == null) {
			return new ModelAndView("/WEB-INF/mail/gmailLogin.jsp");
		}
		request.getSession().setAttribute("mail", mail);
		System.out.println("page load success");
		return new ModelAndView("/WEB-INF/mail/mail.jsp", "mail", mail);
	}

	@RequestMapping(value = "/readJsonHMail.do", method = RequestMethod.GET)
	public void readJsonContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String pass = request.getParameter("pass");
		System.out.println("id : " + id + " : " + pass);
		if (id != null && pass != null) {
			hanmail.setAccountDetails(id, pass);
		}
		System.out.println("Page Loading");
		JSONObject subject = hanmail.getJsonSubject();
		System.out.println("page load success");
		response.getWriter().println(subject);
	}
	
	@RequestMapping(value = "/readHMailContent.do", method = RequestMethod.GET)
	public ModelAndView readMailContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String index = request.getParameter("index");
		if (index == null || index.length() == 0) {
			index = "0";
		}
		System.out.println("index : " + index);
		@SuppressWarnings("unchecked")
		List<GMailDTO> mail = (List<GMailDTO>) request.getSession()
				.getAttribute("mail");
		return new ModelAndView("/WEB-INF/mail/receivemail.jsp", "mail",
				mail.get(Integer.parseInt(index)));
	}

	@RequestMapping(value = "/readJsonHMailContent.do", method = RequestMethod.GET)
	public void readJsonMailContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String index = request.getParameter("index");
		if (index == null || index.length() == 0) {
			index = "0";
		}
		System.out.println("Page Loading");
		JSONObject subject = hanmail.getJsonSubject();
		System.out.println("page load success");
		response.getWriter().println(subject);
	}
	
	@RequestMapping(value = "/hmailLogin.do", method = RequestMethod.POST)
	public ModelAndView gmailLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("Login Call!");
		String id = request.getParameter("id");
		String pass = request.getParameter("pass");
		String hidden = request.getParameter("invis");

		request.getSession().setAttribute("id", id);
		request.getSession().setAttribute("pass", pass);
		// response.sendRedirect("readMail.do");

		hanmail.setAccountDetails(id, pass);
		if (hidden != null && hidden.trim().equals("mobile")) {
			response.sendRedirect("mobile_UI/start.html");
			return null;
			// return new ModelAndView("mobile_UI/start.html");
		}
		List<MailDTO> mail = hanmail.getSubject();
		if (mail == null) {
			System.out.println("login fail!!");
			return new ModelAndView("/gmailLogin.jsp");
		}
		request.getSession().setAttribute("mail", mail);
		System.out.println("Login and page load success");
		return new ModelAndView("/WEB-INF/mail/mail.jsp", "mail", mail);
	}

	@RequestMapping(value = "/sendHMail.do", method = RequestMethod.POST)
	public void sendMail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("call mail write");
		String subject = request.getParameter("subject");
		String content = request.getParameter("content");
		String receiver = request.getParameter("receiver");
		String[] toAddr = null;
		if (receiver.contains(";")) {
			toAddr = receiver.split(";");
		} else {
			toAddr = new String[] { receiver };
		}
		System.out.println("subject :" + subject);
		System.out.println("content : " + content);
		System.out.println("addr : " + toAddr[0]);
		boolean res = hanmail.sendHanmail(toAddr, subject, content);
		if (res) {
			response.getWriter().println("success");
			System.out.println("send success");
		} else {
			response.getWriter().println("fail");
			System.out.println("send fail");
		}
	}

}
